package com.juai.centralkitchen.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.bridge.common.base.BaseActivity;

import com.bridge.common.base.BaseHttpBean;
import com.bridge.common.bean.Loginbean;
import com.bridge.common.body.LoginBody;
import com.bridge.common.dialog.VDialog;
import com.bridge.common.http.RetrofitHelper;
import com.bridge.common.http.ToastUtil;
import com.bridge.common.network.HttpDisposable;
import com.bridge.common.utils.SpUtils;
import com.juai.centralkitchen.R;
import com.juai.centralkitchen.databinding.ActivityLoginBinding;
import com.juai.centralkitchen.jpushreceiver.TagAliasOperatorHelper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ClassName LoginActivity
 * @Description TODO 登录
 * @Author biekangdong
 * @CreateDate 2021/5/12 19:15
 * @Version 1.0
 * @UpdateDate 2021/5/12 19:15
 * @UpdateRemark 更新说明
 */
public class LoginActivity extends BaseActivity {
    ActivityLoginBinding binding;
    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        binding=getBind();

        binding.titleView.setBackVisibility(false);
        binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

    }

    @Override
    protected void setListenner() {
        binding.btnLogin.setOnClickListener(this);

        binding.etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                isEmpty();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                isEmpty();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public void isEmpty(){
        if(!TextUtils.isEmpty(binding.etAccount.getText().toString())&&!TextUtils.isEmpty(binding.etPassword.getText().toString())){
            binding.btnLogin.setBackgroundResource(R.drawable.bg_them_4);
            binding.btnLogin.setEnabled(true);
        }else {
            binding.btnLogin.setBackgroundResource(R.drawable.bg_gray_4);
            binding.btnLogin.setEnabled(false);
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.btn_login:
                httpLogin();
                break;
        }
    }

    public void httpLogin(){
        String username=binding.etAccount.getText().toString();
        String password=binding.etPassword.getText().toString();
        LoginBody loginBody=new LoginBody();
        loginBody.setUsername(username);
        loginBody.setPassword(password);
        RetrofitHelper.getApiService().login(loginBody)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpDisposable<Loginbean>() {
                    @Override
                    protected void onStart() {
                        super.onStart();
                        VDialog.getInstance().showLoadingDialog(mContext);
                    }

                    @Override
                    public void success(Loginbean dataBean) {
                        VDialog.getInstance().hideLoadingDialog();
                        if (dataBean.getCode() == 200) {

                            if (!TextUtils.isEmpty(dataBean.getData().getToken().getAccess_token())) {
                                SpUtils.putData(mContext, SpUtils.ISLOGIN, 1);
                                SpUtils.putData(mContext, SpUtils.TOKEN, dataBean.getData().getToken().getAccess_token());
                                SpUtils.putData(mContext, SpUtils.ISADMIN, dataBean.getData().getIsAdmin());
                                SpUtils.putData(mContext, SpUtils.ACCOUNT, username);

                                startActivity(new Intent(mContext, MainActivity.class));
                                finish();
                            } else {
                                ToastUtil.showToast(mContext, "登录失败");
                            }

                        }else {
                            ToastUtil.showToast(mContext,dataBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        VDialog.getInstance().hideLoadingDialog();
                        ToastUtil.showToast(mContext,msg);
                    }
                });

    }
}
